package com.ruoyi.web.controller.business.goods.factory;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.goods.domain.JbPackageCommodity;
import com.ruoyi.business.goods.domain.JbPackageMain;
import com.ruoyi.business.goods.service.JbPackageCommodityService;
import com.ruoyi.business.goods.service.JbPackageMainService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.system.factory.SysDataScopeFactory;
import com.ruoyi.web.core.JoController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 业务 套餐 工厂
 *
 * @author Jyl
 */
@Component
public class JbPackageFactory extends JoController
{

    @Autowired
    private JbPackageMainService jbPackageMainService;

    @Autowired
    private JbPackageCommodityService jbPackageCommodityService;
    @Autowired
    private SysDataScopeFactory sysDataScopeFactory;

    /**
     * @param page          分页查询对象
     * @param jbPackageMain 套餐对象
     * @return 分页
     */
    public IPage list(PageParam page, JbPackageMain jbPackageMain)
    {
        String channel = getUser().getDept().getChannel();
        if (StrUtil.isNotEmpty(jbPackageMain.getSalesChannel()))
        {
            channel = jbPackageMain.getSalesChannel();
        }
        String ids = sysDataScopeFactory.fnStrDataScope("1");

        QueryWrapper<JbPackageMain> select = new QueryWrapper<>();

        select.lambda().like(StringUtils.isNotEmpty(channel), JbPackageMain::getSalesChannel, channel)
                .and(StringUtils.isNotEmpty(page.getKeyword()), e -> e
                        .like(JbPackageMain::getPackageName, page.getKeyword())
                        .or().like(JbPackageMain::getProductCategory, page.getKeyword())
                )
        ;
        select.and("1002".equals(channel) && StrUtil.isNotEmpty(ids), e -> e
                .apply("id  in (select package_id from jb_package_offline_price jcop where jcop" +
                       ".department_ids ='0')")
                .or()
                .apply(StrUtil.format("id in ( SELECT\n" +
                                      "	package_id \n" +
                                      "FROM\n" +
                                      "	jb_package_offline_price jcop \n" +
                                      "WHERE\n" +
                                      "	jcop.department_ids IS NOT NULL \n" +
                                      "	AND jcop.department_ids <> '' \n" +
                                      "	AND jcop.department_ids <> '0' \n" +
                                      "	AND jcop.department_ids REGEXP '{}' )", ids.replaceAll(",",
                        "|")))
        );

        //拼接套餐商品
        IPage<JbPackageMain> iPage = jbPackageMainService.page(page.b(), select);
        for (JbPackageMain record : iPage.getRecords())
        {
            record.setCommodityList(jbPackageCommodityService.list(new LambdaQueryWrapper<JbPackageCommodity>()
                    .eq(JbPackageCommodity::getPackageId, record.getId())
            ));
        }

        return iPage;
    }

}
